package users

import (
	"net/http"
	"product/common/enumeration"
	"product/common/utils"

	"github.com/zeromicro/go-zero/rest/httpx"
	"product/authorization/serve/internal/logic/users"
	"product/authorization/serve/internal/svc"
	"product/authorization/serve/internal/types"
)

// Check valid mobile
func CheckMobileExistHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.CheckMobileExistReq
		if err := httpx.Parse(r, &req); err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
			return
		}

		l := users.NewCheckMobileExistLogic(r.Context(), svcCtx)
		resp, err := l.CheckMobileExist(&req)
		if err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
			return
		}
		if !resp.Ok {
			httpx.OkJsonCtx(r.Context(), w, utils.Fail(resp.Message))
			return
		}
		httpx.OkJsonCtx(r.Context(), w, utils.Success(resp, resp.Message, enumeration.ProcessingSuccess))
	}
}
